﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;
using System.Drawing;

namespace CommonFunction
{
    public static class FB_Common
    {
        public const UInt16 ARR_DIMENSION = 30;

        public static GAME_STATUS GameStatus;
        public static string DataFilePath;
        public static int SLEEP_SECOND;

        public static STRATEGY STRATEGY_TYPE;
        public static UInt16 BET_RATE = 10;
        public static UInt32 STOP_WINNING = 1000;
        public static UInt32 STOP_LOSING = 100;
        public static UInt16 WAITING_BR = 0;
        public static UInt16 WAITING_EO = 0;
        public static UInt16 WAITING_HL = 0;
        public static UInt16 WAITING_DZ = 0;
        public static UInt16 WAITING_CL = 0;
        public static UInt16 WAITING_NO = 0;
        public static UInt16[,] FOLLOWING_VALUES;
        public static UInt16[,] OTHER_VALUES;

        #region "Location of controls"

        public const UInt16 SPIN_X = 622;
        public const UInt16 SPIN_Y = 621;

        public const UInt16 RESET_X = 622;
        public const UInt16 RESET_Y = 621;

        public const UInt16 RED_X = 310;
        public const UInt16 RED_Y = 470;
        public const UInt16 BLACK_X = 451;
        public const UInt16 BLACK_Y = 470;

        public const UInt16 ODD_X = 171;
        public const UInt16 ODD_Y = 470;
        public const UInt16 EVEN_X = 595;
        public const UInt16 EVEN_Y = 470;

        public const UInt16 LOW_X = 47;
        public const UInt16 LOW_Y = 470;
        public const UInt16 HIGH_X = 731;
        public const UInt16 HIGH_Y = 470;

        public const UInt16 DOZEN1_X = 96;
        public const UInt16 DOZEN2_X = 381;
        public const UInt16 DOZEN3_X = 634;
        public const UInt16 DOZEN_Y = 436;

        public const UInt16 COL1_X = 731;
        public const UInt16 COL1_Y = 285;
        public const UInt16 COL2_X = 758;
        public const UInt16 COL2_Y = 333;
        public const UInt16 COL3_X = 771;
        public const UInt16 COL3_Y = 374;

        public const UInt16 BET_RATE1_X = 250;
        public const UInt16 BET_RATE2_X = 302;
        public const UInt16 BET_RATE3_X = 355;
        public const UInt16 BET_RATE4_X = 407;
        public const UInt16 BET_RATE5_X = 459;
        public const UInt16 BET_RATE6_X = 510;
        public const UInt16 BET_RATE_Y = 612;

        public const UInt16 NO_0_X = 18;
        public const UInt16 NO_0_Y = 308;
        public const UInt16 NO_COL1_X = 79;
        public const UInt16 NO_COL1_Y = 289;
        public const UInt16 NO_COL2_X = 60;
        public const UInt16 NO_COL2_Y = 337;
        public const UInt16 NO_COL3_X = 32;
        public const UInt16 NO_COL3_Y = 387;
        public const UInt16 NO_COL1_MARGIN_X = 55;
        public const UInt16 NO_COL2_MARGIN_X = 58;
        public const UInt16 NO_COL3_MARGIN_X = 63;

        #endregion

        #region "image files"

        public static string ReadyFilePath;
        public static string ResultFilePath;
        public static Bitmap PicReady { get; set; }
        public static IntPtr HANDLER;

        public const UInt16 IMAGE_READY_X = 770;
        public const UInt16 IMAGE_READY_Y = 525;
        public const UInt16 IMAGE_WIDTH = 5;
        public const UInt16 IMAGE_HEIGHT = 5;

        public const UInt16 IMAGE_RESULT_X = 603;
        public const UInt16 IMAGE_RESULT_Y = 3;
        public const UInt16 IMAGE_RESULT_WIDTH = 15;
        public const UInt16 IMAGE_RESULT_HEIGHT = 15;

        #endregion

        public static DateTime PUBLISH_DATE = new DateTime(2013, 12, 7);
        public const UInt16 TRIAL_TERM = 30;

        public static Int16 LAST_NUMBER_RESULT { get; set; }
        public static UInt16 LOST_FOLLOW_BLACK_RED { get; set; }
        public static UInt16 LOST_FOLLOW_HIGH_LOW { get; set; }
        public static UInt16 LOST_FOLLOW_EVEN_ODD { get; set; }
        public static UInt16 WAIT_BLACK { get; set; }
        public static UInt16 WAIT_RED { get; set; }
        public static UInt16 WAIT_HIGH { get; set; }
        public static UInt16 WAIT_LOW { get; set; }
        public static UInt16 WAIT_EVEN { get; set; }
        public static UInt16 WAIT_ODD { get; set; }
        public static UInt16 WAIT_DOZEN1 { get; set; }
        public static UInt16 WAIT_DOZEN2 { get; set; }
        public static UInt16 WAIT_DOZEN3 { get; set; }
        public static UInt16 WAIT_COLUMN1 { get; set; }
        public static UInt16 WAIT_COLUMN2 { get; set; }
        public static UInt16 WAIT_COLUMN3 { get; set; }
        public static UInt16[] WAIT_NUMBER;
        public static UInt32[,] TOTAL_BET_AMOUNT;
        public static double TOTAL_WINNINGS;

        public enum GAME_STATUS
        {
            LOADED = 1,
            STARTED = 2,
            STOPPED = 3
        }

        public enum STRATEGY
        {
            Following = 1,
            Other = 2
        }

        public enum STRATEGIES
        {
            BLACK_RED = 0,
            EVEN_ODD = 1,
            HIGH_LOW = 2,
            DOZENS = 3,
            COLUMNS = 4,
            NUMBERS = 5
        }

        public enum COLUMNS
        {
            C1 = 1,
            C2 = 2,
            C3 = 3
        }

        public enum DOZENS
        {
            D1 = 1,
            D2 = 2,
            D3 = 3
        }

        #region "access configuration"

        public static void InitGlobalParameters(string rootPath)
        {
            FB_Common.DataFilePath = System.IO.Path.Combine(rootPath, ConfigurationManager.AppSettings["DataFilePath"]);
            FB_Common.SLEEP_SECOND = Convert.ToInt32(ConfigurationManager.AppSettings["Sleep"]);

            FB_Common.ReadyFilePath = System.IO.Path.Combine(rootPath, ConfigurationManager.AppSettings["imgReady"]);
            FB_Common.ResultFilePath = System.IO.Path.Combine(rootPath, ConfigurationManager.AppSettings["imgResult"]);
        }

        public static bool InitGameInfo(XmlDocument xmlDoc)
        {
            bool success = false;
            try
            {
                if (xmlDoc.GetDoItFollowing() == "True")
                    STRATEGY_TYPE = STRATEGY.Following;
                else
                    STRATEGY_TYPE = STRATEGY.Other;

                BET_RATE = Convert.ToUInt16(xmlDoc.GetBetRate());
                STOP_WINNING = Convert.ToUInt32(xmlDoc.GetStopWinning());
                STOP_LOSING = Convert.ToUInt32(xmlDoc.GetStopLosing());

                var followingBlackRedValues = xmlDoc.GetFollowingBlackRedValues();
                var followingEvenOddValues = xmlDoc.GetFollowingEvenOddValues();
                var followingHighLowValues = xmlDoc.GetFollowingHighLowValues();

                var otherBlackRedValues = xmlDoc.GetBlackRedValues();
                var otherEvenOddValues = xmlDoc.GetEvenOddValues();
                var otherHighLowValues = xmlDoc.GetHighLowValues();
                var otherColumnValues = xmlDoc.GetColumnValues();
                var otherDozenValues = xmlDoc.GetDozenValues();
                var otherNumberValues = xmlDoc.GetNumberValues();

                var nodeName = string.Empty;

                FOLLOWING_VALUES = new UInt16[3, 30];
                OTHER_VALUES = new UInt16[6, 30];

                WAITING_BR = Convert.ToUInt16(otherBlackRedValues["Wait"].InnerText);
                WAITING_EO = Convert.ToUInt16(otherEvenOddValues["Wait"].InnerText);
                WAITING_HL = Convert.ToUInt16(otherHighLowValues["Wait"].InnerText);
                WAITING_DZ = Convert.ToUInt16(otherDozenValues["Wait"].InnerText);
                WAITING_CL = Convert.ToUInt16(otherColumnValues["Wait"].InnerText);
                WAITING_NO = Convert.ToUInt16(otherNumberValues["Wait"].InnerText);

                for (UInt16 i = 0; i < ARR_DIMENSION; i++)
                {
                    nodeName = string.Format("BR{0}", (i + 1).ToString("00"));
                    FOLLOWING_VALUES[(int)STRATEGIES.BLACK_RED, i] = Convert.ToUInt16(followingBlackRedValues[nodeName].InnerText);
                    OTHER_VALUES[(int)STRATEGIES.BLACK_RED, i] = Convert.ToUInt16(otherBlackRedValues[nodeName].InnerText);

                    nodeName = string.Format("EO{0}", (i + 1).ToString("00"));
                    FOLLOWING_VALUES[(int)STRATEGIES.EVEN_ODD, i] = Convert.ToUInt16(followingEvenOddValues[nodeName].InnerText);
                    OTHER_VALUES[(int)STRATEGIES.EVEN_ODD, i] = Convert.ToUInt16(otherEvenOddValues[nodeName].InnerText);

                    nodeName = string.Format("HL{0}", (i + 1).ToString("00"));
                    FOLLOWING_VALUES[(int)STRATEGIES.HIGH_LOW, i] = Convert.ToUInt16(followingHighLowValues[nodeName].InnerText);
                    OTHER_VALUES[(int)STRATEGIES.HIGH_LOW, i] = Convert.ToUInt16(otherHighLowValues[nodeName].InnerText);

                    nodeName = string.Format("Dozen{0}", (i + 1).ToString("00"));
                    OTHER_VALUES[(int)STRATEGIES.DOZENS, i] = Convert.ToUInt16(otherDozenValues[nodeName].InnerText);

                    nodeName = string.Format("CL{0}", (i + 1).ToString("00"));
                    OTHER_VALUES[(int)STRATEGIES.COLUMNS, i] = Convert.ToUInt16(otherColumnValues[nodeName].InnerText);

                    nodeName = string.Format("NO{0}", (i + 1).ToString("00"));
                    OTHER_VALUES[(int)STRATEGIES.NUMBERS, i] = Convert.ToUInt16(otherNumberValues[nodeName].InnerText);
                }

                //reset variables                
                LAST_NUMBER_RESULT = -1;
                LOST_FOLLOW_BLACK_RED = 0; LOST_FOLLOW_EVEN_ODD = 0; LOST_FOLLOW_HIGH_LOW = 0;
                WAIT_RED = 0; WAIT_BLACK = 0;
                WAIT_HIGH = 0; WAIT_LOW = 0;
                WAIT_EVEN = 0; WAIT_ODD = 0;
                WAIT_DOZEN1 = 0; WAIT_DOZEN2 = 0; WAIT_DOZEN3 = 0;
                WAIT_COLUMN1 = 0; WAIT_COLUMN2 = 0; WAIT_COLUMN3 = 0;
                WAIT_NUMBER = new UInt16[37];
                TOTAL_WINNINGS = 0;
                TOTAL_BET_AMOUNT = new UInt32[6, 37];

                success = true;
            }
            catch { }
            finally
            {
            }
            return success;
        }

        #endregion
    }
}
